const {
    pro,
    str_status,
    timeStr
} = require('../common/pro_mysql');
const {
    createToken,
    verifyToken
} = require('../common/jwt_token');

const mysql = require('mysql')
var con = mysql.createConnection({
    user:'root',
    // password:'PoralMall123...',
    password:'admin123...',
    database:'poral-mall',
    // host: '47.104.24.155',
    host: 'localhost',
    port: 3306
})
// 获取购物车列表
async function GetCartList(req,res){
    let header = req.rawHeaders;
    if(header.indexOf('token') == -1 && header.indexOf('Token') == -1){
        res.send(str_status(0,'token过期'))
    }else{
        let index = header.indexOf('token') == -1 ? header.indexOf('Token') : header.indexOf('token');
        let [token] = header.splice(Number(index)+1,1);
        let {msg} =await verifyToken(token);
        let sql = `select * from cart where user_id=${msg.ID}`;
        let data = await pro(sql);
        res.send(data)
    }
}

// 修改购物车数量
async function EditCartNum(req,res){
    let header = req.rawHeaders;
    if(header.indexOf('token') == -1 && header.indexOf('Token') == -1){
        res.send(str_status(0,'token过期'))
    }else{
        let index = header.indexOf('token') == -1 ? header.indexOf('Token') : header.indexOf('token');
        let [token] = header.splice(Number(index)+1,1);
        let {msg} =await verifyToken(token);
        let {
            address_id,
            number
        } = req.body;
        if(!address_id && !(number > 0)){
            res.status(400).send(str_status(0,'参数错误',[]))
            return
        }
        let sql = `update cart set pro_num=${number} where ID=${address_id} and user_id=${msg.ID}`;
        let data = await pro(sql);
        res.send(data)
    }
}

// 删除购物车
async function DelCartNum(req,res){
    let header = req.rawHeaders;
    if(header.indexOf('token') == -1 && header.indexOf('Token') == -1){
        res.send(str_status(0,'token过期'))
    }else{
        let index = header.indexOf('token') == -1 ? header.indexOf('Token') : header.indexOf('token');
        let [token] = header.splice(Number(index)+1,1);
        let {msg} =await verifyToken(token);
        let {
            address_id
        } = req.body;
        if(!address_id){
            res.status(400).send(str_status(0,'参数错误',[]))
            return
        }
        let sql = `delete from cart where ID=${address_id} and user_id=${msg.ID}`;
        let data = await pro(sql);
        res.send(data)
    }
}
// 添加购物车
async function AddCartList(req,res){
    let header = req.rawHeaders;
    if(header.indexOf('token') == -1 && header.indexOf('Token') == -1){
        res.send(str_status(0,'token过期'))
    }else{
        let index = header.indexOf('token') == -1 ? header.indexOf('Token') : header.indexOf('token');
        let [token] = header.splice(Number(index)+1,1);
        let {msg} =await verifyToken(token);
        let {
            pro_title,
            pro_img,
            pro_price,
            pro_num,
            pro_sku,
            pro_id = "null"
        } = req.body;
        pro_sku = con.escape(JSON.stringify(pro_sku));
        if(pro_title && pro_img && pro_price && pro_num && pro_sku){
            let sql = `insert into cart (pro_title,pro_img,pro_price,pro_num,createTime,pro_sku,user_id,pro_id) values ("${pro_title}","${pro_img}","${pro_price}","${pro_num}","${Date.now()}","${pro_sku}",${msg.ID},${pro_id})`
            let data = await pro(sql);
            res.send(data)
        }else{
            res.status(400).send(str_status(0,'参数错误'))
        }
    }
}

module.exports = {
    GetCartList,
    EditCartNum,
    DelCartNum,
    AddCartList
}